//
//  CHSViewController.m
//  ZXCategories
//
//  Created by 海参 on 12/07/2021.
//  Copyright (c) 2021 海参. All rights reserved.
//

#import "CHSViewController.h"
#import <Masonry/Masonry.h>
#import <ZXCategoriesUtil/ZXCategoriesUtil.h>
#import "CHSRuntimeViewController.h"
#import "CHSDeallocViewController.h"
#import "CHSSortViewController.h"
#import "CHSUIViewController.h"
#import "CHSUI2ViewController.h"
#import "CHSWindowViewController.h"


@interface CHSViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (atomic, strong) NSMutableArray *dataMArray;

@end

@implementation CHSViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"主线程%@",[NSThread currentThread]);
    [self setUI];
    [self addMas];
    [self setData];
    
    CGFloat total = [NSFileManager zx_diskSpaceGB];
    CGFloat total1 = [NSFileManager zx_diskFreeSpaceGB];
    CGFloat total2 = [NSFileManager zx_diskUsedSpaceGB];
//    NSError *error = nil;

    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
}

- (void)addMas{

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {      make.left.mas_equalTo(self.view.mas_left).with.offset(15);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view.mas_top).with.offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
}

#pragma mark - Data

- (void)setData
{
   NSArray *arr = @[
        @{@"kvo":@"KVC和KVO例子"},
        @{@"sort":@"排序例子"},
        @{@"dealloc":@"dealloc释放例子"},
        @{@"runtime":@"runtime例子"},
        @{@"ui1":@"ui例子1:ui布局"},
        @{@"ui2":@"ui例子2：测试scrollView、tableView是否能同时上下滚动、水平滚动"},
        @{@"ui3":@"ui例子3：UIWindow的各种window获取"},

    ];
    self.dataMArray = [NSMutableArray arrayWithArray:arr];
//    self.dataMArray = [NSMutableArray array];
}


#pragma mark -

- (UITableView *)tableView
{
    if (!_tableView)
    {
        UITableView *view = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        view.backgroundColor = self.view.backgroundColor;
        view.delegate = self;
        view.dataSource = self;
        view.showsVerticalScrollIndicator = NO;
        [view registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        view.estimatedRowHeight = 87;
        view.rowHeight = UITableViewAutomaticDimension;
        if (@available(iOS 15.0, *)) {
            view.sectionHeaderTopPadding = 0;
        } else {
            // Fallback on earlier versions
        }
        _tableView = view;
    }
    return _tableView;
}

#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    CHSMyRedpacketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
//    [cell setData:nil];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataMArray objectAtIndex:indexPath.row];
    NSString *value = [dic.allValues firstObject];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = value;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 12;
    }
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 12;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataMArray objectAtIndex:indexPath.row];
    NSString *key = [dic.allKeys firstObject];
    if ([key isEqualToString:@"kvo"]) {
        [self.navigationController zx_pushStoryboardViewControllerWithStoryboardName:@"Main" identifier:@"CHSKVOViewController" withData:nil];
        return;
    }
    else if ([key isEqualToString:@"sort"]) {
        CHSSortViewController *vc = [CHSSortViewController new];
        vc.navTitle = [dic.allValues firstObject];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([key isEqualToString:@"dealloc"]) {
        CHSDeallocViewController *vc = [CHSDeallocViewController new];
        vc.navTitle = [dic.allValues firstObject];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([key isEqualToString:@"runtime"]) {
        CHSRuntimeViewController *vc = [CHSRuntimeViewController new];
        vc.navTitle = [dic.allValues firstObject];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([key isEqualToString:@"ui1"]) {
        CHSUIViewController *vc = [CHSUIViewController new];
        vc.navTitle = [dic.allValues firstObject];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([key isEqualToString:@"ui2"]) {
        CHSUI2ViewController *vc = [CHSUI2ViewController new];
        vc.navTitle = [dic.allValues firstObject];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([key isEqualToString:@"ui3"]) {
        CHSWindowViewController *vc = [CHSWindowViewController new];
        vc.navTitle = [dic.allValues firstObject];
        [self.navigationController pushViewController:vc animated:YES];
    }
 
}

@end
